package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.Activity;
import com.tjetc.entity.ActivityFlower;
import com.tjetc.entity.User;
import com.tjetc.service.IActivityFlowerService;
import com.tjetc.service.IActivityService;
import com.tjetc.service.IUserMessageService;
import com.tjetc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/activityFlower")
public class ActivityFlowerController {
    @Autowired
    private IActivityFlowerService activityFlowerService;

    @Autowired
    private IActivityService activityService;


    @Autowired
    private IUserService userService;

    @RequestMapping("/sub")
    public Result<?>  sub(Integer uid,Integer aid,String text){
        ActivityFlower activityFlower = new ActivityFlower();
        activityFlower.setUserId(uid);
        activityFlower.setActivityId(aid);
        activityFlower.setText(text);
        activityFlower.setState(0);
        boolean save = activityFlowerService.save(activityFlower);
        if (save){
            return Result.success();
        }else{
            return Result.fail();
        }

    }
    @RequestMapping("/getFlowerList")
    public List<ActivityFlower> sub(Integer uid, Integer aid){

        QueryWrapper<ActivityFlower> qw=new QueryWrapper<>();

        qw.eq("activity_id",aid);

        List<ActivityFlower> activityFlowerList=activityFlowerService.list(qw);
        for (ActivityFlower activityFlower : activityFlowerList) {
            activityFlower.setActivity(activityService.getById(activityFlower.getActivityId()));
            activityFlower.setUser(userService.getById(activityFlower.getUserId()));
        }
        return activityFlowerList;

    }

    @RequestMapping("/list")
    public PageInfo<ActivityFlower> list(@RequestParam(defaultValue = "1") Integer curPage,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Integer aid)
                                   {
        System.out.println("curPage = " + curPage);
        System.out.println("pageSize = " + pageSize);
        System.out.println("aid = " + aid);
        return activityFlowerService.lists(curPage, pageSize, aid);
    }


    @RequestMapping("/check")
    public Result<?> check(Integer id) {
        boolean i = activityFlowerService.check(id);

        if (i) {
            return Result.success("成功");
        } else {
            return Result.fail("失败");
        }

    }

}
